package view;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;

import model.*;
import controller.*;


import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;



public class VehiculoAction extends ActionSupport implements Preparable, ModelDriven<KlsVehiculo> {

	
	// ATRIBUTOS
	
	
	
	private static final long serialVersionUID = 1L;
	private KlsVehiculo vehiculo = new KlsVehiculo();
	
	private KlsVehiculoManager vehiculoManager;
	private int id;
	private List<KlsVehiculo> vehiculoList;
	private String zona;
	private int idVehiculo;
	private KlsConductorManager conductorManager;
	private List<KlsConductor> conductorList;
	private KlsEstadoVehiculoManager estadoVehiculoManager;
	private List<KlsEstadoVehiculo> estadoVehiculoList;
	private KlsZonaEntregaManager zonaEntregaManager;
	private List<KlsZonaEntrega> zonaEntregaList;
	private int idUsuario;
	private int idConductor;
	private int idEstadoVehiculo;
	private int idZonaEntrega;
		
	// FIN ATRIBUTOS


	////////////////////////////////////////////////////////////////////////////////////////
	//CONSTRUCTOR
	////////////////////////////////////////////////////////////////////////////////////////

	public VehiculoAction() {
		vehiculoManager = new KlsVehiculoManager();
		estadoVehiculoManager = new KlsEstadoVehiculoManager();
		conductorManager = new KlsConductorManager();
		zonaEntregaManager = new KlsZonaEntregaManager();
		
	}
	
	public KlsVehiculo getModel() {
		return vehiculo;
}
	

	////////////////////////////////////////////////////////////////////////////////////////
	// metodo prepare por defecto primero en ejecutarse independientemente del resto.
	////////////////////////////////////////////////////////////////////////////////////////
	
	public void prepare() throws Exception {
		this.setConductorList(conductorManager.list());
		this.setZonaEntregaList(zonaEntregaManager.list());
		System.out.println("prepare");
	}

	
	// METODOS
	@Override
	public String execute() {
    	//setVehiculoList(vehiculoManager.list());
        System.out.println("execute called");
        return SUCCESS;
    }
	
	public String list() {
	     try {
	    	this.vehiculoList = vehiculoManager.list();
	        System.out.println("execute called");
	     }    
	     catch (Exception e) {
	        e.printStackTrace();
	        return ERROR;
	    }
	        return SUCCESS;
	}
	
	
	
	public String add() throws Exception {
		
		//leo varibles de sesion  
		HttpServletRequest request = ServletActionContext.getRequest();
		
		setIdConductor( Integer.valueOf( request.getParameter("idConductor")));
		setIdZonaEntrega( Integer.valueOf( request.getParameter("idZonaEntrega")));
		setIdUsuario(Integer.valueOf(  ActionContext.getContext().getSession().get("loggedInIdUser").toString() ));
		
		System.out.println(getVehiculo());
        vehiculoManager.add(getVehiculo(), idConductor, idUsuario, idZonaEntrega, idEstadoVehiculo);
		return SUCCESS;		
	}
	
	public String delete() {
	try {	
		HttpServletRequest request = ServletActionContext.getRequest();
		String s = request.getParameter("idVehiculo");
		setIdVehiculo( Integer.parseInt(s));
		vehiculo = vehiculoManager.delete(this.getIdVehiculo());   		
		
	} catch (Exception e) {
        e.printStackTrace();
        return ERROR;
	}
        return SUCCESS;
	}
	
	public String load() {
		this.setConductorList(conductorManager.list());
		this.setZonaEntregaList(zonaEntregaManager.list());
	try {
    		HttpServletRequest request = ServletActionContext.getRequest();
    		String s = request.getParameter("idVehiculo");
    		setIdVehiculo( Integer.parseInt(s));
    		vehiculo = vehiculoManager.load(this.getIdVehiculo());
    		
    	} catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
    	return SUCCESS;
    }
	
	public String update() {
		
		HttpServletRequest request = ServletActionContext.getRequest();
		setIdEstadoVehiculo( Integer.valueOf( request.getParameter("idEstadoVehiculo")));
		setIdConductor( Integer.valueOf( request.getParameter("idConductor")));
		setIdZonaEntrega( Integer.valueOf( request.getParameter("idZonaEntrega")));
		setIdUsuario(Integer.valueOf(  ActionContext.getContext().getSession().get("loggedInIdUser").toString() ));
    	try {
    		vehiculo = vehiculoManager.update(vehiculo, idConductor, idUsuario, idZonaEntrega , idEstadoVehiculo);
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS;
    }
	
	public String zonaPorVehiculo() {
		this.setId(idVehiculo);
		zona = vehiculoManager.getZonaDelVEhiculo(this.id);
		
		//this.setVehiculo(vehiculoManager.load(id));
		//pesoMax = vehiculo.calcularPesoMax().toString();
		//volumenMax = vehiculo.calcularVolumenMax().toString();
	return SUCCESS;
	}
	
	////////////////////////////////////////////////////////////////////////////
	// Geters y Seters
	////////////////////////////////////////////////////////////////////////////
	public KlsConductorManager getConductorManager() {
		return conductorManager;
	}


	public void setConductorManager(KlsConductorManager conductorManager) {
		this.conductorManager = conductorManager;
	}


	public KlsEstadoVehiculoManager getEstadoVehiculoManager() {
		return estadoVehiculoManager;
	}


	public void setEstadoVehiculoManager(
			KlsEstadoVehiculoManager estadoVehiculoManager) {
		this.estadoVehiculoManager = estadoVehiculoManager;
	}


	public KlsZonaEntregaManager getZonaEntregaManager() {
		return zonaEntregaManager;
	}


	public void setZonaEntregaManager(KlsZonaEntregaManager zonaEntregaManager) {
		this.zonaEntregaManager = zonaEntregaManager;
	}


	public int getIdVehiculo() {
		return idVehiculo;
	}

	public void setIdVehiculo(int idVehiculo) {
		this.idVehiculo = idVehiculo;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}


	public KlsVehiculo getVehiculo() {
		return vehiculo;
	}

	public void setVehiculo(KlsVehiculo vehiculo) {
		this.vehiculo = vehiculo;
	}

	public KlsVehiculoManager getVehiculoManager() {
		return vehiculoManager;
	}

	public void setVehiculoManager(KlsVehiculoManager vehiculoManager) {
		this.vehiculoManager = vehiculoManager;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public List<KlsVehiculo> getVehiculoList() {
		return vehiculoList;
	}

	public void setVehiculoList(List<KlsVehiculo> vehiculoList) {
		this.vehiculoList = vehiculoList;
	}

	/**
	 * @return the zona
	 */
	public String getZona() {
		return zona;
	}

	/**
	 * @param zona the zona to set
	 */
	public void setZona(String zona) {
		this.zona = zona;
	}
/*
	public String getVolumenMax() {
		return volumenMax;
	}

	public void setVolumenMax(String volumenMax) {
		this.volumenMax = volumenMax;
	}

	public String getPesoMax() {
		return pesoMax;
	}

	public void setPesoMax(String pesoMax) {
		this.pesoMax = pesoMax;
	}
	
	*/


	/**
	 * @return the conductorList
	 */
	public List<KlsConductor> getConductorList() {
		return conductorList;
	}


	/**
	 * @param conductorList the conductorList to set
	 */
	public void setConductorList(List<KlsConductor> conductorList) {
		this.conductorList = conductorList;
	}


	/**
	 * @return the estadoVehiculoList
	 */
	public List<KlsEstadoVehiculo> getEstadoVehiculoList() {
		return estadoVehiculoList;
	}


	/**
	 * @param estadoVehiculoList the estadoVehiculoList to set
	 */
	public void setEstadoVehiculoList(List<KlsEstadoVehiculo> estadoVehiculoList) {
		this.estadoVehiculoList = estadoVehiculoList;
	}


	/**
	 * @return the zonaEntregaList
	 */
	public List<KlsZonaEntrega> getZonaEntregaList() {
		return zonaEntregaList;
	}


	/**
	 * @param zonaEntregaList the zonaEntregaList to set
	 */
	public void setZonaEntregaList(List<KlsZonaEntrega> zonaEntregaList) {
		this.zonaEntregaList = zonaEntregaList;
	}


	/**
	 * @return the idUsuario
	 */
	public int getIdUsuario() {
		return idUsuario;
	}


	/**
	 * @param idUsuario the idUsuario to set
	 */
	public void setIdUsuario(int idUsuario) {
		this.idUsuario = idUsuario;
	}


	/**
	 * @return the idConductor
	 */
	public int getIdConductor() {
		return idConductor;
	}


	/**
	 * @param idConductor the idConductor to set
	 */
	public void setIdConductor(int idConductor) {
		this.idConductor = idConductor;
	}

	/**
	 * @return the idZonaEntrega
	 */
	public int getIdZonaEntrega() {
		return idZonaEntrega;
	}

	/**
	 * @param idZonaEntrega the idZonaEntrega to set
	 */
	public void setIdZonaEntrega(int idZonaEntrega) {
		this.idZonaEntrega = idZonaEntrega;
	}

	/**
	 * @return the idEstadoVehiculo
	 */
	public int getIdEstadoVehiculo() {
		return idEstadoVehiculo;
	}

	/**
	 * @param idEstadoVehiculo the idEstadoVehiculo to set
	 */
	public void setIdEstadoVehiculo(int idEstadoVehiculo) {
		this.idEstadoVehiculo = idEstadoVehiculo;
	}
	
	
	// FIN METODOS

}
	
	
	
	
	
	